package com.linciping.plugin.base;

import com.intellij.codeInsight.CodeInsightActionHandler;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.SyntheticElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.kotlin.asJava.classes.KtUltraLightClass;
import org.jetbrains.kotlin.psi.KtClass;
import org.jetbrains.kotlin.psi.KtFile;

public class SingleClassKotlinGenerateAction extends BaseKotlinGenerateAction {

    public SingleClassKotlinGenerateAction() {
    }

    public SingleClassKotlinGenerateAction(CodeInsightActionHandler codeInsightActionHandler) {
        super(codeInsightActionHandler);
    }

    protected KtUltraLightClass getTargetClass(Editor editor, PsiFile psiFile) {
        int offset = editor.getCaretModel().getOffset();
        PsiElement psiElement = psiFile.findElementAt(offset);
        if (psiElement == null) return null;
        final KtUltraLightClass target = PsiTreeUtil.getParentOfType(psiElement, KtUltraLightClass.class);
        return target instanceof SyntheticElement ? null : target;
    }

    @Override
    protected boolean isValidForKtFile(KtFile ktFile, Editor editor) {
        int ktClassNum = ktFile.getClasses().length;
        return ktClassNum == 1;
    }
}
